<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSqlQueryModel Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSqlQueryModel Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlQueryModel class provides a read-only data model for SQL
result sets. <a href="#details">More...</a></p>

<p>Inherits <a href="qabstracttablemodel.html">QAbstractTableModel</a>.</p><p>Inherited by <a href="qsqltablemodel.html">QSqlTableModel</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqlquerymodel.html#QSqlQueryModel">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" />bool <b><a href="qsqlquerymodel.html#canFetchMore">canFetchMore</a></b> (<i>self</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qsqlquerymodel.html#columnCount">columnCount</a></b> (<i>self</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" />QVariant <b><a href="qsqlquerymodel.html#data">data</a></b> (<i>self</i>, QModelIndex&#160;<i>item</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#fetchMore">fetchMore</a></b> (<i>self</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" />QVariant <b><a href="qsqlquerymodel.html#headerData">headerData</a></b> (<i>self</i>, int&#160;<i>section</i>, Qt.Orientation&#160;<i>orientation</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</li><li><div class="fn" />QModelIndex <b><a href="qsqlquerymodel.html#indexInQuery">indexInQuery</a></b> (<i>self</i>, QModelIndex&#160;<i>item</i>)</li><li><div class="fn" />bool <b><a href="qsqlquerymodel.html#insertColumns">insertColumns</a></b> (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" />QSqlError <b><a href="qsqlquerymodel.html#lastError">lastError</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlQuery <b><a href="qsqlquerymodel.html#query">query</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#queryChange">queryChange</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlRecord <b><a href="qsqlquerymodel.html#record">record</a></b> (<i>self</i>, int&#160;<i>row</i>)</li><li><div class="fn" />QSqlRecord <b><a href="qsqlquerymodel.html#record-2">record</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquerymodel.html#removeColumns">removeColumns</a></b> (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" />int <b><a href="qsqlquerymodel.html#rowCount">rowCount</a></b> (<i>self</i>, QModelIndex&#160;<i>parent</i>&#160;=&#160;QModelIndex())</li><li><div class="fn" />bool <b><a href="qsqlquerymodel.html#setHeaderData">setHeaderData</a></b> (<i>self</i>, int&#160;<i>section</i>, Qt.Orientation&#160;<i>orientation</i>, QVariant&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#setLastError">setLastError</a></b> (<i>self</i>, QSqlError&#160;<i>error</i>)</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#setQuery">setQuery</a></b> (<i>self</i>, QSqlQuery&#160;<i>query</i>)</li><li><div class="fn" /><b><a href="qsqlquerymodel.html#setQuery-2">setQuery</a></b> (<i>self</i>, QString&#160;<i>query</i>, QSqlDatabase&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSqlQueryModel class provides a read-only data model for SQL
result sets.</p>
<p>QSqlQueryModel is a high-level interface for executing SQL
statements and traversing the result set. It is built on top of the
lower-level <a href="qsqlquery.html">QSqlQuery</a> and can be used
to provide data to view classes such as <a href="qtableview.html">QTableView</a>. For example:</p>
<pre class="cpp">
     <span class="type">QSqlQueryModel</span> <span class="operator">*</span>model <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSqlQueryModel</span>;
     model<span class="operator">-</span><span class="operator">&gt;</span>setQuery(<span class="string">"SELECT name, salary FROM employee"</span>);
     model<span class="operator">-</span><span class="operator">&gt;</span>setHeaderData(<span class="number">0</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>Horizontal<span class="operator">,</span> tr(<span class="string">"Name"</span>));
     model<span class="operator">-</span><span class="operator">&gt;</span>setHeaderData(<span class="number">1</span><span class="operator">,</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">.</span>Horizontal<span class="operator">,</span> tr(<span class="string">"Salary"</span>));

     <span class="type"><a href="qtableview.html">QTableView</a></span> <span class="operator">*</span>view <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtableview.html">QTableView</a></span>;
     view<span class="operator">-</span><span class="operator">&gt;</span>setModel(model);
     view<span class="operator">-</span><span class="operator">&gt;</span>show();
</pre>
<p>We set the model's query, then we set up the labels displayed in
the view header.</p>
<p>QSqlQueryModel can also be used to access a database
programmatically, without binding it to a view:</p>
<pre class="cpp">
     <span class="type">QSqlQueryModel</span> model;
     model<span class="operator">.</span>setQuery(<span class="string">"SELECT * FROM employee"</span>);
     <span class="type">int</span> salary <span class="operator">=</span> model<span class="operator">.</span>record(<span class="number">4</span>)<span class="operator">.</span>value(<span class="string">"salary"</span>)<span class="operator">.</span>toInt();
</pre>
<p>The code snippet above extracts the <tt>salary</tt> field from
record 4 in the result set of the query <tt>SELECT * from
employee</tt>. Assuming that <tt>salary</tt> is column 2, we can
rewrite the last line as follows:</p>
<pre class="cpp">
     <span class="type">int</span> salary <span class="operator">=</span> model<span class="operator">.</span>data(model<span class="operator">.</span>index(<span class="number">4</span><span class="operator">,</span> <span class="number">2</span>))<span class="operator">.</span>toInt();
</pre>
<p>The model is read-only by default. To make it read-write, you
must subclass it and reimplement <a href="qabstractitemmodel.html#setData">setData</a>() and <a href="qabstractitemmodel.html#flags">flags</a>(). Another option is to
use <a href="qsqltablemodel.html">QSqlTableModel</a>, which
provides a read-write model based on a single database table.</p>
<p>The <a href="sql-querymodel.html">sql/querymodel</a> example
illustrates how to use QSqlQueryModel to display the result of a
query. It also shows how to subclass QSqlQueryModel to customize
the contents of the data before showing it to the user, and how to
create a read-write model based on QSqlQueryModel.</p>
<p>If the database doesn't return the amount of selected rows in a
query, the model will fetch rows incrementally. See <a href="qsqlquerymodel.html#fetchMore">fetchMore</a>() for more
information.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlQueryModel" />QSqlQueryModel.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates an empty <a href="qsqlquerymodel.html">QSqlQueryModel</a> with the given
<i>parent</i>.</p>


<h3 class="fn"><a name="canFetchMore" />bool QSqlQueryModel.canFetchMore (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#canFetchMore">QAbstractItemModel.canFetchMore</a>().</p>
<p>Returns true if it is possible to read more rows from the
database. This only affects databases that don't report back the
size of a query (see <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>()).</p>
<p><i>parent</i> should always be an invalid <a href="qmodelindex.html">QModelIndex</a>.</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#fetchMore">fetchMore</a>().</p>


<h3 class="fn"><a name="clear" />QSqlQueryModel.clear (<i>self</i>)</h3><p>Clears the model and releases any acquired resource.</p>


<h3 class="fn"><a name="columnCount" />int QSqlQueryModel.columnCount (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#columnCount">QAbstractItemModel.columnCount</a>().</p>


<h3 class="fn"><a name="data" />QVariant QSqlQueryModel.data (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>item</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#data">QAbstractItemModel.data</a>().</p>
<p>Returns the value for the specified <i>item</i> and
<i>role</i>.</p>
<p>If <i>item</i> is out of bounds or if an error occurred, an
invalid <a href="qvariant.html">QVariant</a> is returned.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#lastError">lastError</a>().</p>


<h3 class="fn"><a name="fetchMore" />QSqlQueryModel.fetchMore (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#fetchMore">QAbstractItemModel.fetchMore</a>().</p>
<p>Fetches more rows from a database. This only affects databases
that don't report back the size of a query (see <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>()).</p>
<p>To force fetching of the entire database, you can use the
following:</p>
<pre class="cpp">
 <span class="keyword">while</span> (myModel<span class="operator">-</span><span class="operator">&gt;</span>canFetchMore())
     myModel<span class="operator">-</span><span class="operator">&gt;</span>fetchMore();
</pre>
<p><i>parent</i> should always be an invalid <a href="qmodelindex.html">QModelIndex</a>.</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#canFetchMore">canFetchMore</a>().</p>


<h3 class="fn"><a name="headerData" />QVariant QSqlQueryModel.headerData (<i>self</i>, int&#160;<i>section</i>, <a href="qt.html#Orientation-enum">Qt.Orientation</a>&#160;<i>orientation</i>, int&#160;<i>role</i>&#160;=&#160;Qt.DisplayRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#headerData">QAbstractItemModel.headerData</a>().</p>
<p>Returns the header data for the given <i>role</i> in the
<i>section</i> of the header with the specified
<i>orientation</i>.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#setHeaderData">setHeaderData</a>().</p>


<h3 class="fn"><a name="indexInQuery" /><a href="qmodelindex.html">QModelIndex</a> QSqlQueryModel.indexInQuery (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>item</i>)</h3><p>Returns the index of the value in the database result set for
the given <i>item</i> in the model.</p>
<p>The return value is identical to <i>item</i> if no columns or
rows have been inserted, removed, or moved around.</p>
<p>Returns an invalid model index if <i>item</i> is out of bounds
or if <i>item</i> does not point to a value in the result set.</p>
<p><b>See also</b> <a href="qsqltablemodel.html#indexInQuery">QSqlTableModel.indexInQuery</a>(),
<a href="qsqlquerymodel.html#insertColumns">insertColumns</a>(),
and <a href="qsqlquerymodel.html#removeColumns">removeColumns</a>().</p>


<h3 class="fn"><a name="insertColumns" />bool QSqlQueryModel.insertColumns (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#insertColumns">QAbstractItemModel.insertColumns</a>().</p>
<p>Inserts <i>count</i> columns into the model at position
<i>column</i>. The <i>parent</i> parameter must always be an
invalid <a href="qmodelindex.html">QModelIndex</a>, since the model
does not support parent-child relationships.</p>
<p>Returns true if <i>column</i> is within bounds; otherwise
returns false.</p>
<p>By default, inserted columns are empty. To fill them with data,
reimplement <a href="qsqlquerymodel.html#data">data</a>() and
handle any inserted column separately:</p>
<pre class="cpp">
 <span class="type"><a href="qvariant.html">QVariant</a></span> MyModel<span class="operator">.</span><a href="qsqlquerymodel.html#data">data</a>(<span class="keyword">const</span> <span class="type"><a href="qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>item<span class="operator">,</span> <span class="type">int</span> role) <span class="keyword">const</span>
 {
     <span class="keyword">if</span> (item<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> m_specialColumnNo) {
         <span class="comment">// handle column separately</span>
     }
     <span class="keyword">return</span> <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span><span class="operator">.</span>data(item<span class="operator">,</span> role);
 }
</pre>
<p><b>See also</b> <a href="qsqlquerymodel.html#removeColumns">removeColumns</a>().</p>


<h3 class="fn"><a name="lastError" /><a href="qsqlerror.html">QSqlError</a> QSqlQueryModel.lastError (<i>self</i>)</h3><p>Returns information about the last error that occurred on the
database.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#setLastError">setLastError</a>() and <a href="qsqlquerymodel.html#query">query</a>().</p>


<h3 class="fn"><a name="query" /><a href="qsqlquery.html">QSqlQuery</a> QSqlQueryModel.query (<i>self</i>)</h3><p>Returns the <a href="qsqlquery.html">QSqlQuery</a> associated
with this model.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#setQuery">setQuery</a>().</p>


<h3 class="fn"><a name="queryChange" />QSqlQueryModel.queryChange (<i>self</i>)</h3><p>This virtual function is called whenever the query changes. The
default implementation does nothing.</p>
<p><a href="qsqlquerymodel.html#query">query</a>() returns the new
query.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#query">query</a>()
and <a href="qsqlquerymodel.html#setQuery">setQuery</a>().</p>


<h3 class="fn"><a name="record" /><a href="qsqlrecord.html">QSqlRecord</a> QSqlQueryModel.record (<i>self</i>, int&#160;<i>row</i>)</h3><p>Returns the record containing information about the fields of
the current query. If <i>row</i> is the index of a valid row, the
record will be populated with values from that row.</p>
<p>If the model is not initialized, an empty record will be
returned.</p>
<p><b>See also</b> <a href="qsqlrecord.html#isEmpty">QSqlRecord.isEmpty</a>().</p>


<h3 class="fn"><a name="record-2" /><a href="qsqlrecord.html">QSqlRecord</a> QSqlQueryModel.record (<i>self</i>)</h3><p>This is an overloaded function.</p>
<p>Returns an empty record containing information about the fields
of the current query.</p>
<p>If the model is not initialized, an empty record will be
returned.</p>
<p><b>See also</b> <a href="qsqlrecord.html#isEmpty">QSqlRecord.isEmpty</a>().</p>


<h3 class="fn"><a name="removeColumns" />bool QSqlQueryModel.removeColumns (<i>self</i>, int&#160;<i>column</i>, int&#160;<i>count</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#removeColumns">QAbstractItemModel.removeColumns</a>().</p>
<p>Removes <i>count</i> columns from the model starting from
position <i>column</i>. The <i>parent</i> parameter must always be
an invalid <a href="qmodelindex.html">QModelIndex</a>, since the
model does not support parent-child relationships.</p>
<p>Removing columns effectively hides them. It does not affect the
underlying <a href="qsqlquery.html">QSqlQuery</a>.</p>
<p>Returns true if the columns were removed; otherwise returns
false.</p>


<h3 class="fn"><a name="rowCount" />int QSqlQueryModel.rowCount (<i>self</i>, <a href="qmodelindex.html">QModelIndex</a>&#160;<i>parent</i>&#160;=&#160;QModelIndex())</h3><p>Reimplemented from <a href="qabstractitemmodel.html#rowCount">QAbstractItemModel.rowCount</a>().</p>
<p>If the database supports returning the size of a query (see
<a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>()),
the amount of rows of the current query is returned. Otherwise,
returns the amount of rows currently cached on the client.</p>
<p><i>parent</i> should always be an invalid <a href="qmodelindex.html">QModelIndex</a>.</p>
<p>This function was introduced in Qt 4.1.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#canFetchMore">canFetchMore</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>().</p>


<h3 class="fn"><a name="setHeaderData" />bool QSqlQueryModel.setHeaderData (<i>self</i>, int&#160;<i>section</i>, <a href="qt.html#Orientation-enum">Qt.Orientation</a>&#160;<i>orientation</i>, QVariant&#160;<i>value</i>, int&#160;<i>role</i>&#160;=&#160;Qt.EditRole)</h3><p>Reimplemented from <a href="qabstractitemmodel.html#setHeaderData">QAbstractItemModel.setHeaderData</a>().</p>
<p>Sets the caption for a horizontal header for the specified
<i>role</i> to <i>value</i>. This is useful if the model is used to
display data in a view (e.g., <a href="qtableview.html">QTableView</a>).</p>
<p>Returns true if <i>orientation</i> is <a href="qt.html#Orientation-enum">Qt.Horizontal</a> and the
<i>section</i> refers to a valid section; otherwise returns
false.</p>
<p>Note that this function cannot be used to modify values in the
database since the model is read-only.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#headerData">headerData</a>() and <a href="qsqlquerymodel.html#data">data</a>().</p>


<h3 class="fn"><a name="setLastError" />QSqlQueryModel.setLastError (<i>self</i>, <a href="qsqlerror.html">QSqlError</a>&#160;<i>error</i>)</h3><p>Protected function which allows derived classes to set the value
of the last error that occurred on the database to
<i>error</i>.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#lastError">lastError</a>().</p>


<h3 class="fn"><a name="setQuery" />QSqlQueryModel.setQuery (<i>self</i>, <a href="qsqlquery.html">QSqlQuery</a>&#160;<i>query</i>)</h3><p>Resets the model and sets the data provider to be the given
<i>query</i>. Note that the query must be active and must not be
isForwardOnly().</p>
<p><a href="qsqlquerymodel.html#lastError">lastError</a>() can be
used to retrieve verbose information if there was an error setting
the query.</p>
<p><b>Note:</b> Calling setQuery() will remove any inserted
columns.</p>
<p><b>See also</b> <a href="qsqlquerymodel.html#query">query</a>(),
<a href="qsqlquery.html#isActive">QSqlQuery.isActive</a>(),
<a href="qsqlquery.html#setForwardOnly">QSqlQuery.setForwardOnly</a>(),
and <a href="qsqlquerymodel.html#lastError">lastError</a>().</p>


<h3 class="fn"><a name="setQuery-2" />QSqlQueryModel.setQuery (<i>self</i>, QString&#160;<i>query</i>, <a href="qsqldatabase.html">QSqlDatabase</a>&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</h3><p>This is an overloaded function.</p>
<p>Executes the query <i>query</i> for the given database
connection <i>db</i>. If no database (or an invalid database) is
specified, the default connection is used.</p>
<p><a href="qsqlquerymodel.html#lastError">lastError</a>() can be
used to retrieve verbose information if there was an error setting
the query.</p>
<p>Example:</p>
<pre class="cpp">
 <span class="type"><a href="qsqlquerymodel.html">QSqlQueryModel</a></span> model;
 model<span class="operator">.</span><a href="qsqlquerymodel.html#setQuery">setQuery</a>(<span class="string">"select * from MyTable"</span>);
 <span class="keyword">if</span> (model<span class="operator">.</span>lastError()<span class="operator">.</span>isValid())
     <a href="qtcore.html#qDebug">qDebug</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> model<span class="operator">.</span><a href="qsqlquerymodel.html#lastError">lastError</a>();
</pre>
<p><b>See also</b> <a href="qsqlquerymodel.html#query">query</a>(),
<a href="qsqlquerymodel.html#queryChange">queryChange</a>(), and
<a href="qsqlquerymodel.html#lastError">lastError</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.9.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt&#160;4.8.0</td></tr></table></div></address></body></html>